package com.changgou.order.controller;

import com.alibaba.fastjson.JSON;
import com.changgou.order.config.TokenDecode;
import com.changgou.order.pojo.OrderItem;
import com.changgou.order.service.CartService;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.jwt.Jwt;
import org.springframework.security.jwt.JwtHelper;
import org.springframework.security.jwt.crypto.sign.RsaVerifier;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 描述
 *
 * @author ljh
 * @version 1.0
 * @package com.changgou.order.controller *
 * @Date 2019-12-5
 * @since 1.0
 */
@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;


    @Autowired
    private TokenDecode tokenDecode;

    /**
     * 添加购物车
     *
     * @param id  要购买的商品的ID(SKU的ID)
     * @param num 数量
     * @return
     */
    @RequestMapping("/add")
    public Result add(Long id, Integer num) {




        //写死
        //String username = "szitheima";


        Map<String, String> userInfo = tokenDecode.getUserInfo();

        //通过key来获取对应的key值
        String username = userInfo.get("username");

        cartService.add(username,id, num);

        return new Result(true, StatusCode.OK, "添加购物车成功");

    }

    //查询出当前登录的用户的购物车列表

    @RequestMapping("/list")
    public Result<List<OrderItem>> findCartList(){

        Map<String, String> userInfo = tokenDecode.getUserInfo();


        //用json转换为map格式来获得数据
       // Map<String,String> map = JSON.parseObject(claims, Map.class);


        //通过key来获取对应的key值
        String username = userInfo.get("username");
       List<OrderItem> cartList =  cartService.findCartList(username);
        return new Result(true, StatusCode.OK, "查看购物车列表成功",cartList);
    }
}
